import {
  getCustomerList,
  editCustomerList
} from '@/api/customer'

const state = {
  customerData: [],
  customerDataCount: 0
}

const mutations = {
  GET_CUSTOMER_DATA: (state, data) => {
    state.customerData = data
  },
  GET_CUSTOMER_DATA_COUNT: (state, data) => {
    state.customerDataCount = data
  }
}

const actions = {
  async getCustomerData({ commit }, playload) {
    let data = {}
    let newData = []
    if (Object.keys(playload).length > 0) {
      data = playload
    }
    const resp = await getCustomerList(data)
    resp.data.data.rows.map((item) => {
      newData = [...newData, {
        id: item.id,
        name: item.name,
        tel: item.tel,
        carttype0: item.carttype0,
        state: item.state,
        ctime: item.ctime,
        ordertime: item.ordertime ? new Date(item.ordertime) : '',
        des: item.des,
        type0: item.type0
      }]
    })
    // for (let i = 0; i < 5; i++) {
    //   newData = [...newData, {
    //     name: '王小虎',
    //     tel: '13775237256',
    //     type: '法拉利',
    //     ctime: '2019-04-25 14:58:43',
    //     appointime: '2019-04-25 14:58:43',
    //     des: '微信付了定金'
    //   }]
    // }
    commit('GET_CUSTOMER_DATA', newData)
    commit('GET_CUSTOMER_DATA_COUNT', resp.data.data.total)
    return resp
  },
  async editCustomerData({ commit }, playload) {
    const resp = await editCustomerList(playload)
    return resp
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
